package com.cssy.yd.basic.web;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cssy.base.web.BaseController;
import com.cssy.base.web.BodyMsg;
import com.cssy.yd.basic.entity.BasicAreaDO;
import com.cssy.yd.basic.service.IBasicAreaService;
import com.plat.helper.JqGridHandler;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.GlobalResLoader;
import com.plat.util.OperationFlag;
import com.plat.util.SessionUtil;
import com.plat.util.json.OutJsonGrid;
import com.yuendong.sy.po.code.SyCode;
import com.yuendong.sy.service.code.SyCodeType;
import com.yuendong.sy.service.code.impl.SyCodeService;

@Controller
@RequestMapping("/yd/basic/area")
public class BasicAreaController extends BaseController<BasicAreaDO>{

	@Resource(name="syCodeService")
	private SyCodeService syCodeService;
	
	@Resource(name="basicAreaService")
	private IBasicAreaService basicAreaService;
	
	private static final String MANAGER = "/yd/basic/area/AreaManage";//列表页面
	private static final String EIDT = "/yd/basic/area/AreaOperation";//添加、修改、显示页面	
	@Override
	public String manager(HttpServletRequest request, Model model)
			throws Exception {
		
		return MANAGER;
	}

	@Override
	public void list(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JqGridHandler hander=new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		if(scs.getOrderBy()==null||"".equals(scs.getOrderBy()))
			 scs.setOrderBy("dctime desc");
		SearchResult result =	basicAreaService.findListByExample(scs);
		OutJsonGrid out=new OutJsonGrid(result);
		out.printJSon(response);
		
	}

	@Override
	public String operationForm(HttpServletRequest request,@PathVariable String id,
			@PathVariable String flag, Model model) throws Exception {
//		SearchConditions scs = new SearchConditions();
		
		if(OperationFlag.ADD.equals(flag)){//我新增加时操作
			model.addAttribute("flag","add");
		}else{
			BasicAreaDO area=	basicAreaService.findListById(id);
			model.addAttribute("area", area);
			model.addAttribute("flag","edit");
		}
		return EIDT;
	}

	@Override
	public Map<String, Object> save(HttpServletRequest request,
			BasicAreaDO curAP, BindingResult result, Model model)
			throws Exception {
			BodyMsg bodyMap=BodyMsg.getInstance();
		
		try {
			if(curAP.getId()!=null && !"".equals(curAP.getId())){//修改
				basicAreaService.update(curAP, SessionUtil.getUser(request));
				bodyMap.isSuccess();
				bodyMap.configWebInfor(GlobalResLoader.getOpsuccess());
				bodyMap.configWebId(curAP.getId());
			}else{//新增
				BasicAreaDO bad = new BasicAreaDO();
				bad.setAreaname(curAP.getAreaname());
				bad.setUseSystem(curAP.getUseSystem());//使用系统约束add by huang.xuming[2013-09-13]
				bad.setRemoved(Short.parseShort("0"));//使用系统约束add by huang.xuming[2013-09-13]
				List<BasicAreaDO> nameInfo = basicAreaService.findListByExample(bad);
				
				if(nameInfo!=null && !nameInfo.isEmpty()){
					for (BasicAreaDO ba : nameInfo ){
						if(ba.getAreaname().equals(bad.getAreaname())
								&& ba.getUseSystem().equals(bad.getUseSystem())//使用系统约束add by huang.xuming[2013-09-13]
								){
							bodyMap.isFail();
							bodyMap.configWebInfor("对不起您输入的名称已存在");
							return bodyMap.modelMap;
						}
					}
				}
				
				if(curAP.getAreacode()!=null&&!"".equals(curAP.getAreacode())){
					BasicAreaDO bdo = new BasicAreaDO();
					bdo.setAreacode(curAP.getAreacode());
					bdo.setUseSystem(curAP.getUseSystem());
					bdo.setRemoved(Short.parseShort("0"));//使用系统约束add by huang.xuming[2013-09-13]
					List<BasicAreaDO> codeInfo = basicAreaService.findListByExample(bdo);
					
					curAP.setRemoved(Short.parseShort("0"));
					curAP.setIsactive(Short.parseShort("1"));
					boolean flag = false; 
					if(codeInfo!=null && !codeInfo.isEmpty()){
						for (BasicAreaDO ba : codeInfo ){
							if(ba.getAreacode().equals(bdo.getAreacode())//使用系统约束add by huang.xuming[2013-09-13]
									&& ba.getUseSystem().equals(bad.getUseSystem())//使用系统约束add by huang.xuming[2013-09-13]
									){
								flag = true;
							}
						}
					}
					if (flag){
							bodyMap.isFail();
							bodyMap.configWebInfor("对不起您输入的编码已存在");
							return bodyMap.modelMap;
					}
					
				}else{
					curAP.setAreacode(syCodeService.getCode("AREA","AREA",SyCode.DATE_YYMM,5));
				}
				BasicAreaDO cdo=basicAreaService.save(curAP, SessionUtil.getUser(request));
				bodyMap.isSuccess();
				bodyMap.configWebInfor(GlobalResLoader.getOpsuccess());
				bodyMap.configWebId(cdo.getId());
			}
		} catch (Exception e) {			
			e.printStackTrace();
			bodyMap.isFail();
			bodyMap.configWebInfor(GlobalResLoader.getOpfailed()+e.getMessage());
		}
		return bodyMap.modelMap;
	}

	@Override
	public Map<String, Object> delete(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		BodyMsg bodyMap=BodyMsg.getInstance();
		try{
			String ids = request.getParameter("ids");
			String[] fields=ids.split(",");
			
			for(String field : fields){
				basicAreaService.delete(field, SessionUtil.getUser(request));
			}
		
			bodyMap.isSuccess(GlobalResLoader.getOpsuccess());
		}catch(Exception e){
			e.printStackTrace();
			bodyMap.isFail(GlobalResLoader.getOpfailed()+e.getMessage());
		}
		return bodyMap.modelMap;
	}

}
